package com.wyn.ai.controller;

import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;

/**
 * @author rise
 */
@RestController
@RequestMapping("/ai")
public class ChatController {

    @Resource
    private ChatClient chatClient;


//    @RequestMapping("/chat")
//    public String chat(@RequestParam("question") String question) {
//        return chatClient.prompt().user(question).call().content();
//    }

    /**
     * 流式输出➕中文解码
     * @param question 问题
     * @return
     */
    @RequestMapping(value = "/chat",produces = "text/html;charset=utf-8")
    public Flux<String> chat(@RequestParam("question") String question, @RequestParam("chatId")String charId) {
        return chatClient.prompt()
                .user(question)
                .advisors(advisorSpec -> advisorSpec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, charId))
                .stream()
                .content();
    }
}
